package lsy.Servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lsy.Dao.DaoFactory;
import lsy.entity.Course;
import lsy.entity.Teacher;
import lsy.tools.PageInfo;
import lsy.tools.PathUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/course")
public class CourseServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if("list".equals(method)) {
            this.list(request, response);
        }else if("add".equals(method)) {
            this.add(request, response);
        }else if("v_add".equals(method)) {
            this.v_add(request, response);
        }else if("edit".equals(method)) {
            this.findById(request, response);
        }else if("editsubmit".equals(method)) {
            this.editsubmit(request, response);
        }else if("delete".equals(method)) {
            this.delete(request, response);
        }

    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        try {
            DaoFactory.getInstance().getCourseDao().delete(Integer.parseInt(id));
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"course?method=list");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void editsubmit(HttpServletRequest request, HttpServletResponse response) {
        Integer courseId= Integer.valueOf(request.getParameter("courseId"));
        String courseName = request.getParameter("courseName");
        //老师ID
        Integer tid = Integer.valueOf(request.getParameter("tid"));
        Course course = new Course();
        course.setCourseName(courseName);
        course.setCourseId(courseId);
        //new一个Teacher对象，然后设置ID，然后再设置course对象的setTeacher关联 一下即可
        Teacher teacher = new Teacher();
        teacher.setTid(tid);
        course.setTeacher(teacher);
        try {
            DaoFactory.getInstance().getCourseDao().update(course);
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"course?method=list");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private void findById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        PageInfo<Teacher> pageInfo = new PageInfo<>(1);
        pageInfo.setPageSize(1000);
        try {
            Course course = DaoFactory.getInstance().getCourseDao().findById(Integer.parseInt(id));
            pageInfo = DaoFactory.getInstance().getTeacherDao().list(null, pageInfo);
            request.setAttribute("course", course);
            request.setAttribute("teachers", pageInfo.getList());
            request.getRequestDispatcher("course/update.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void v_add(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException  {
        PageInfo<Teacher> pageInfo = new PageInfo<>(1);
        pageInfo.setPageSize(1000);
        try {
            pageInfo = DaoFactory.getInstance().getTeacherDao().list(null, pageInfo);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        request.setAttribute("teachers", pageInfo.getList());
        request.getRequestDispatcher("course/add.jsp").forward(request, response);
    }

    private void add(HttpServletRequest request, HttpServletResponse response) {
        Integer courseId= Integer.parseInt(request.getParameter("courseId"));
        String courseName = request.getParameter("courseName");
        //老师ID
        Integer tid = Integer.parseInt(request.getParameter("tid"));
        Course course = new Course();
        course.setCourseId(courseId);
        course.setCourseName(courseName);

        //new一个Teacher对象，然后设置ID，然后再设置course对象的setTeacher关联 一下即可
        Teacher teacher = new Teacher();
        teacher.setTid(tid);
        course.setTeacher(teacher);
        try {
            DaoFactory.getInstance().getCourseDao().add(course);
            //直接重定向到列表页面
            response.sendRedirect(PathUtils.getBasePath(request)+"course?method=list");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) {
        //当前页码
        Integer pageNo = getIntParameter(request, "pageNo");
        //查询条件
        Integer courseId = getIntParameter(request, "courseId");
        String courseName = request.getParameter("courseName");
        String teaName = request.getParameter("teaName");
        String teaUsername = request.getParameter("teaUsername");

        Course course = new Course();
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        Teacher teacher = new Teacher();
        teacher.setTeaName(teaName);
        teacher.setTeaUsername(teaUsername);
        course.setTeacher(teacher);

        //构造了一个pageInfo对象
        PageInfo<Course> pageInfo = new PageInfo<>(pageNo);
        try {
            pageInfo = DaoFactory.getInstance().getCourseDao().list(course,pageInfo);
        } catch (SQLException e1) {
            e1.printStackTrace();
        }
        try {
            request.setAttribute("pageInfo", pageInfo);
            //回写到页面
            request.setAttribute("course", course);
            request.getRequestDispatcher("course/list.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public Integer getIntParameter(HttpServletRequest request,String name) {
        if(StringUtils.isNoneBlank(request.getParameter(name))) {
            return Integer.parseInt(request.getParameter(name));
        }else {
            return null;
        }
    }
}